<script setup>
import { ref } from 'vue'
import { QuestionFilled } from '@element-plus/icons-vue'
import { policiesCreateService } from '@/api/policies'
import { useRouter } from 'vue-router'
import { insertTab, checkPoliciesContent } from '@/utils/policies'

const router = useRouter()

const form = ref()

const policiesModel = ref({
  name: '',
  content: ''
})

const rules = {
  name: [
    { required: true, message: '请输入策略名', trigger: 'submit' }
  ],
  content: [
    { required: true, message: '请输入策略内容', trigger: 'submit' },
    {
      validator: (rule, value, callback) => {
        checkPoliciesContent(value, callback)
      },
      trigger: 'submit'
    }
  ]
}

const createPolicies = async () => {
  await form.value.validate()
  policiesModel.value.content = JSON.stringify(JSON.parse(policiesModel.value.content), null, 0)
  await policiesCreateService(policiesModel.value)
  router.push('/policies')
}

</script>

<template>
  <mt-create-form>
    <el-form ref="form" :model="policiesModel" :rules="rules" size="large" label-width="auto" @submit.prevent>
      <el-form-item>
        <el-icon size="30">
          <svg t="1733367675151" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="13948"><path d="M512 108.7488S296.192 251.3408 168.1408 173.7216c-2.9184 0 21.3504 484.9664 60.1088 562.5856 38.8096 77.6192 283.6992 178.944 283.6992 178.944s240.0768-98.9184 281.088-178.944c41.0112-80.0256 60.8256-560.64 63.0272-562.5856C713.216 258.0992 512 108.7488 512 108.7488z m78.0288 396.032v28.2112h-60.3136v49.8176l48.4864 21.7088v15.4112h-48.4864v80.5376l-36.0448 48.7936V442.3168c-36.0448-8.2944-62.9248-40.4992-62.9248-79.104 0-44.8512 36.352-81.2544 81.2544-81.2544s81.2544 36.352 81.2544 81.2544c0 38.7584-27.136 71.1168-63.488 79.2064v62.3104h60.2624z" p-id="13949"></path></svg>
        </el-icon>
        <span style="font-weight: bold; margin-left: 8px">创建策略</span>
      </el-form-item>
      <el-form-item prop="name" label="策略名" label-position="top">
        <el-input v-model="policiesModel.name" name="name"></el-input>
      </el-form-item>
      <el-form-item prop="content" label="写入策略" label-position="top">
        <el-input v-model="policiesModel.content" @keydown.tab.prevent="insertTab" name="content" type="textarea" resize="none" :autosize="{minRows: 18}">
          
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          @click="createPolicies"
          type="default"
          color="black"
          size="large"
          style="margin-top: 20px; padding: 20px; position: absolute; right: 0"
          auto-insert-space
        >
          创建策略
        </el-button>
      </el-form-item>
    </el-form>

    <template #tip>
      <p style="display: flex; align-items: center">
        <el-icon size="30"><QuestionFilled /></el-icon
        ><span style="margin-left: 5px; font-weight: bold">了解有关策略的更多信息</span>
      </p>
      MT 使用基于策略的访问控制来定义经过身份验证的用户有权访问的授权操作和资源。每个策略都描述一个或多个操作和条件，
      这些操作和条件概述了用户的权限。每个用户只能访问内置角色明确授予的资源和操作。默认情况下，MT 拒绝访问任何其他资源或操作。
      <pre><code>
{
    "statement": [
        {
            "action": [
                "get"
            ],
            "resource": [
                "/mt/**"
            ]
        }
    ]
}
    </code></pre>
    </template>
  </mt-create-form>
</template>

<style lang="scss" scoped></style>
